// 音效工具类 - 使用 Web Audio API 生成简单音效
export class SoundUtils {
  static audioContext= null
  static enabled= true

  // 初始化音频上下文
  static init() {
    if (!this.audioContext) {
      try {
        this.audioContext = new (window.AudioContext || (window as any).webkitAudioContext)()
      } catch (e) {
        console.warn('Web Audio API not supported:', e)
      }
    }
  }

  // 设置音效开关
  static setEnabled(enabled) {
    this.enabled = enabled
  }

  // 播放正确答案音效（愉悦的上升音调）
  static playCorrect() {
    if (!this.enabled || !this.audioContext) return
    
    const oscillator = this.audioContext.createOscillator()
    const gainNode = this.audioContext.createGain()
    
    oscillator.connect(gainNode)
    gainNode.connect(this.audioContext.destination)
    
    // 上升音调：C5 -> E5 -> G5
    oscillator.frequency.setValueAtTime(523.25, this.audioContext.currentTime) // C5
    oscillator.frequency.setValueAtTime(659.25, this.audioContext.currentTime + 0.1) // E5
    oscillator.frequency.setValueAtTime(783.99, this.audioContext.currentTime + 0.2) // G5
    
    oscillator.type = 'sine'
    gainNode.gain.setValueAtTime(0.1, this.audioContext.currentTime)
    gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.3)
    
    oscillator.start(this.audioContext.currentTime)
    oscillator.stop(this.audioContext.currentTime + 0.3)
  }

  // 播放错误答案音效（低沉的下降音调）
  static playWrong() {
    if (!this.enabled || !this.audioContext) return
    
    const oscillator = this.audioContext.createOscillator()
    const gainNode = this.audioContext.createGain()
    
    oscillator.connect(gainNode)
    gainNode.connect(this.audioContext.destination)
    
    // 下降音调：G4 -> D4
    oscillator.frequency.setValueAtTime(392.00, this.audioContext.currentTime) // G4
    oscillator.frequency.setValueAtTime(293.66, this.audioContext.currentTime + 0.2) // D4
    
    oscillator.type = 'sawtooth'
    gainNode.gain.setValueAtTime(0.08, this.audioContext.currentTime)
    gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.4)
    
    oscillator.start(this.audioContext.currentTime)
    oscillator.stop(this.audioContext.currentTime + 0.4)
  }

  // 播放按钮点击音效（短促的点击声）
  static playClick() {
    if (!this.enabled || !this.audioContext) return
    
    const oscillator = this.audioContext.createOscillator()
    const gainNode = this.audioContext.createGain()
    
    oscillator.connect(gainNode)
    gainNode.connect(this.audioContext.destination)
    
    oscillator.frequency.setValueAtTime(800, this.audioContext.currentTime)
    oscillator.type = 'square'
    gainNode.gain.setValueAtTime(0.05, this.audioContext.currentTime)
    gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.1)
    
    oscillator.start(this.audioContext.currentTime)
    oscillator.stop(this.audioContext.currentTime + 0.1)
  }

  // 播放合成成功音效（上升的和弦）
  static playMerge() {
    if (!this.enabled || !this.audioContext) return
    
    // 播放和弦：C5 + E5 + G5
    const frequencies = [523.25, 659.25, 783.99]
    
    frequencies.forEach((freq, index) => {
      const oscillator = this.audioContext!.createOscillator()
      const gainNode = this.audioContext!.createGain()
      
      oscillator.connect(gainNode)
      gainNode.connect(this.audioContext!.destination)
      
      oscillator.frequency.setValueAtTime(freq, this.audioContext!.currentTime)
      oscillator.type = 'sine'
      gainNode.gain.setValueAtTime(0.03, this.audioContext!.currentTime + index * 0.05)
      gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext!.currentTime + 0.5)
      
      oscillator.start(this.audioContext!.currentTime + index * 0.05)
      oscillator.stop(this.audioContext!.currentTime + 0.5)
    })
  }

  // 播放场景切换音效（柔和的过渡音）
  static playTransition() {
    if (!this.enabled || !this.audioContext) return
    
    const oscillator = this.audioContext.createOscillator()
    const gainNode = this.audioContext.createGain()
    
    oscillator.connect(gainNode)
    gainNode.connect(this.audioContext.destination)
    
    oscillator.frequency.setValueAtTime(440, this.audioContext.currentTime) // A4
    oscillator.frequency.exponentialRampToValueAtTime(880, this.audioContext.currentTime + 0.3) // A5
    
    oscillator.type = 'sine'
    gainNode.gain.setValueAtTime(0.04, this.audioContext.currentTime)
    gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.3)
    
    oscillator.start(this.audioContext.currentTime)
    oscillator.stop(this.audioContext.currentTime + 0.3)
  }

  // 播放轮次结束音效（胜利音调）
  static playRoundEnd() {
    if (!this.enabled || !this.audioContext) return
    
    const oscillator = this.audioContext.createOscillator()
    const gainNode = this.audioContext.createGain()
    
    oscillator.connect(gainNode)
    gainNode.connect(this.audioContext.destination)
    
    // 胜利音调：C5 -> G5 -> C6
    oscillator.frequency.setValueAtTime(523.25, this.audioContext.currentTime) // C5
    oscillator.frequency.setValueAtTime(783.99, this.audioContext.currentTime + 0.15) // G5
    oscillator.frequency.setValueAtTime(1046.50, this.audioContext.currentTime + 0.3) // C6
    
    oscillator.type = 'triangle'
    gainNode.gain.setValueAtTime(0.06, this.audioContext.currentTime)
    gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.6)
    
    oscillator.start(this.audioContext.currentTime)
    oscillator.stop(this.audioContext.currentTime + 0.6)
  }
}
